package com.mingqi.enetconnected.util.date;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by li on 2018/8/13.
 * WeChat 18571658038
 * author LiWei
 */

public class DateUtils {

    public static final DateFormat msSDF = new SimpleDateFormat("mm:ss");

    /**
     * 将长整形时间转换为字符串日期
     *
     * @param longDate
     * @param dateFormat
     * @return
     */
    public static String parseLong2String(long longDate, DateFormat dateFormat) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(longDate);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 将字符串日期转换为长整形时间
     *
     * @param dateStr
     * @param dateFormat
     * @return
     * @throws ParseException
     */
    public static long parseString2Long(String dateStr, DateFormat dateFormat) throws ParseException {

        Date parse = dateFormat.parse(dateStr);
        long time = parse.getTime();

        return time;
    }

    public static List<DateInfo> getMonthDateList() {
        return getDateList(30);
    }

    public static List<DateInfo> getDateList(int total) {
        Calendar calendar = Calendar.getInstance();
        List<DateInfo> data = new ArrayList<>();
        data.add(new DateInfo(getMMDDate(calendar.getTime()), getDayOfWeek(calendar)));
        for (int i = 0; i < total; i++) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
            data.add(new DateInfo(getMMDDate(calendar.getTime()), getDayOfWeek(calendar)));
        }
        return data;
    }

    public static String getDayOfWeek(Calendar calendar) {
        String today = "";
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case Calendar.MONDAY:
                today = ResourceUtils.getString(R.string.monday);
                break;
            case Calendar.TUESDAY:
                today = ResourceUtils.getString(R.string.tuesday);
                break;
            case Calendar.WEDNESDAY:
                today = ResourceUtils.getString(R.string.wednesday);
                break;
            case Calendar.THURSDAY:
                today = ResourceUtils.getString(R.string.thursday);
                break;
            case Calendar.FRIDAY:
                today = ResourceUtils.getString(R.string.friday);
                break;
            case Calendar.SATURDAY:
                today = ResourceUtils.getString(R.string.saturday);
                break;
            case Calendar.SUNDAY:
                today = ResourceUtils.getString(R.string.sunday);
                break;
        }

        return today;
    }

    public static String getMMDDate(Date date) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * @param sDate
     * @return
     */
    public static Date parseStringToDate(String sDate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            return sdf.parse(sDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isRightTime(String leftTime, String rightTime) {
        String today = getMMDDate(new Date());
        Date left = parseStringToDate(today + " " + leftTime);
        Date right = parseStringToDate(today + " " + rightTime);
        if (left == null || right == null) {
            return false;
        }
        return right.after(left);
    }
}
